package com.world.cat.controller.sys;

import com.world.cat.model.sys.Menu;
import com.world.cat.service.sys.MenuService;
import com.world.cat.service.sys.ResourceService;
import com.world.common.pojo.Result;
import com.world.common.pojo.WorldPage;
import com.world.common.pojo.tree.ZTreeNode;
import com.world.common.util.SessionUtil;
import com.world.common.util.SysUtil;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单管理
 */
@Controller
@RequestMapping(value = "/sys/menu", name = "菜单管理")
@Component
public class MenuController {
    @Resource
    private MenuService menuService;
    @Resource
    private ResourceService resourceService;

    @RequestMapping(value = "/home")
    public String home(Model model) {
        return "sys/menu/home";
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public Object save(Menu menu) {
        if (menu.getSeq() == null) {
            return new Result(false, "序号不能为空");
        }
        if (!SysUtil.isAllNotEmpty(menu.getName(), menu.getParentId())) {
            return new Result(false, "菜单名不能为空");
        }
        Menu menuDb = menuService.saveMenu(menu);
        ZTreeNode treeNode = new ZTreeNode();
        treeNode.setChecked(false);
        treeNode.setName(menuDb.getName());
        treeNode.setId(menuDb.getId());
        treeNode.setpId(menuDb.getParentId());
        treeNode.setUrl(menuDb.getUrl());
        return new Result(true, "", treeNode);
    }

    @RequestMapping(value = "/queryMenuList")
    @ResponseBody
    public Object queryMenuList(String id) {
        if (id == null) {
            id = "0";
        }
        Result result = new Result(true, "Success", menuService.queryMenuList(id, SessionUtil.getUserName()));
        return result.getData();
    }

    @RequestMapping(value = "/menuTree", name = "-菜单树")
    @ResponseBody
    public Object menuTree() {
        ZTreeNode treeNode = menuService.getMenuTree();
        List<ZTreeNode> nodes = new ArrayList<>();
        nodes.add(treeNode);
        return nodes;
    }

    @RequestMapping(value = "/move", name = "转移")
    @ResponseBody
    public Object move(String sourceId, String targetId) {
        menuService.move(sourceId, targetId);
        return new Result(true);
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(WorldPage myPage, Model model, String resourceName) {
        Page<Menu> page = menuService.getMenuPage(myPage.getPageRequest(), resourceName);
        Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map
        jsonMap.put("total", page.getTotalElements());//total键 存放总记录数，必须的
        jsonMap.put("rows", page.getContent());//rows键 存放每页记录 list
        return jsonMap;
    }

    @RequestMapping(value = "/del", name = "删除")
    @ResponseBody
    public Object del(String[] ids) {
        Result result = menuService.delMenu(ids);
        return result;
    }

    @RequestMapping(value = "/info")
    @ResponseBody
    public Object info(String id) {
        Menu menu = menuService.get(id);
        return new Result(true, "成功", menu);
    }

    @RequestMapping(value = "/add", name = "添加")
    public void add() {
    }

    @RequestMapping(value = "/edit", name = "编辑")
    public void edit() {
    }

    //提取所有controller里面的页面
    @RequestMapping(value = "/resourcePage")
    @ResponseBody
    public Object resourcePage() {
        ZTreeNode zTreeNode = resourceService.getResourcePage();
        return zTreeNode.getChildren();
    }
}
